<template>
  <!-- 下拉刷新 -->
  <van-pull-refresh v-model="refreshing" @refresh="refresh">
    <!-- 加载列表 -->
    <van-list
      v-model:loading="loading"
      :finished="finished"
      :offset="10"
      finished-text="没有更多了"
      @load="load"
    >
      <!-- 商品卡片 -->
      <van-card
        v-for="(item, key) in list"
        :thumb="item.product.thumbs_text"
        @click-thumb="toProductInfo(item.proid)"
        :key="key"
      >
        <template #title>
          <div class="title">{{ item.product.name }}</div>
        </template>

        <template #desc>
          <van-highlight
            :keywords="`${item.total}`"
            :source-string="`人数：${item.total} 人`"
            highlight-class="people"
          />
          拼团时间：{{ item.createtime_text }}(开始) ~
          {{ item.endtime_text }}(结束)<br />
          拼团价格：￥{{ item.product.team }}<br />
        </template>

        <template #footer>
          <van-button
            type="danger"
            size="small"
            @click="TeamJoin(item.id, item.proid)"
            >参与拼团</van-button
          >
          <van-button type="warning" size="small" @click="TeamInfo(item.id)"
            >查看详情</van-button
          >
        </template>
      </van-card>
    </van-list>
  </van-pull-refresh>
  <tabbar />
</template>
<script setup>
import tabbar from "@/components/common/tabbar.vue";

const { proxy } = getCurrentInstance();

//下拉刷新的数据
let list = ref([]); //数据列表
let page = ref(1); //分页
let loading = ref(false); //加载状态
let finished = ref(false); //是否无更多数据
let refreshing = ref(false); //下拉刷新

//加载
const load = async () => {
  if (refreshing.value) {
    refreshing.value = false;
    list.value = [];
  }
  loading.value = false;
  // 发送请求
  var result = await proxy.$POST({
    url: "/teambus/index",
    params: { page: page.value },
  });

  loading.value = false;
  refreshing.value = false;

  if (result.code == 0) {
    finished.value = true;
    return false;
  }

  console.log(result);

  list.value = list.value.concat(result.data.list);

  if (list.value.length >= result.data.count) {
    finished.value = true;
  }
  page.value++;
};

//下拉刷新
const refresh = () => {
  page.value = 1;
  refreshing.value = true;
  load();
};

//跳转到购物车详情页面
const toProductInfo = (proid) =>
  proxy.$router.push({ path: "/product/info", query: { proid } });

//跳转到拼团详情
const TeamInfo = (teamid) => proxy.$router.push({ path: "/team/info", query: { teamid } });

//参与拼团
const TeamJoin = (teamid, proid) => {
    proxy.$cookies.set('teamid',teamid)
    proxy.$router.push({path:'/product/info',query:{'proid':proid}})
}
</script>